package controllers.listeners.feature;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import controllers.shared.GetInputLoop;

import objects.Constituent;
import objects.Feature;
import objects.Lexicon;

import views.ViewFeatures;

/* Adds a new feature to the data structure */
/* actionPerformed when you click "Feature" button in Features Module */
public class AddFeatureActionListener implements ActionListener {
	
	private ViewFeatures view;
	
	public AddFeatureActionListener() {
		view = null;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String inputName = null;
		String inputValue = null;
		
		try {
			/* Get feature name */
			GetInputLoop.setInputDialog("Enter the name of the new feature.", "Add New Feature", JOptionPane.OK_CANCEL_OPTION);
			GetInputLoop.setErrorDialog("Please enter a name.", "Error");
			inputName = GetInputLoop.getInput();
			
			if (inputName == null) {
				return;
			}
			
			/* Get feature default value */
			GetInputLoop.setInputDialog("Enter the default value of '" + inputName + "'.", "Add Default Value", JOptionPane.OK_CANCEL_OPTION);
			GetInputLoop.setErrorDialog("Please enter a default value.", "Error");
			inputValue = GetInputLoop.getInput();
			
			if (inputValue == null) {
				return;
			}
			
			Feature feature = new Feature();
			feature.setName(inputName);
			feature.setValue(null);
			feature.setEditable(true);
			feature.addValueListItem(inputValue);
			
			if(view.getSelectedFeatureType().equals("Semantic Rep")) {
				Constituent constituent = view.getCurrentConstituent();
				constituent.addFeatureListItem(feature);
			}
			else if(view.getSelectedFeatureType().equals("Lexicon")) {
				Lexicon lexicon = view.getCurrentLexicon();
				lexicon.addFeatureListItem(feature);
			}
			
			view.addRows(feature);
		}
		catch (Exception ex) {
			return;
		}
	}
	
	public void setView(ViewFeatures view) {
		this.view = view;
	}
	
}
